package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.PositionGroup;
import com.jiezuo.auth.mapper.PositionGroupMapper;
import com.jiezuo.auth.service.PositionGroupService;
import com.jiezuo.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description 岗位组
 * @author 庞留杰
 * @date 2024/03/12 14:51
 * @version V1.0
 **/
@Transactional
@Slf4j
@Service
public class PositionGroupServiceImpl extends ServiceImpl<PositionGroupMapper, PositionGroup> implements PositionGroupService {

    @Autowired
    PositionGroupMapper mapper;

    @Override
    public IPage<PositionGroup> findPage(Page initMpPage, PositionGroup positionGroup) {
        QueryWrapper<PositionGroup> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(positionGroup.getName())) queryWrapper.like("name", positionGroup.getName());
        queryWrapper.eq("is_delete", "0");
        Page page = this.page(initMpPage, queryWrapper);
        List<PositionGroup> list = page.getRecords();
        list.forEach(it -> {
            String positions = mapper.findAllPositionById(it.getId());
            it.setPositions(positions);
        });
        return page;
    }

    @Override
    public List<PositionGroup> findList(PositionGroup positionGroup) {
        QueryWrapper<PositionGroup> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(positionGroup.getName())) queryWrapper.like("name", positionGroup.getName());
        queryWrapper.eq("is_delete", "0");
        List<PositionGroup> list = this.list(queryWrapper);
        list.forEach(it -> {
            String positions = mapper.findAllPositionById(it.getId());
            it.setPositions(positions);
        });
        return list;
    }

    @Override
    public PositionGroup findById(String id) {
        return this.getById(id);
    }

    @Override
    public boolean add(PositionGroup positionGroup) {
        return this.save(positionGroup);
    }

    @Override
    public boolean update(PositionGroup positionGroup) {
        return this.updateById(positionGroup);
    }

    @Override
    public boolean delete(String id) {
        PositionGroup positionGroup = new PositionGroup();
        positionGroup.setId(id);
        positionGroup.setIs_delete("1");
        return this.updateById(positionGroup);
    }
}
